/*	This file is part of AstroAttack.
*
*	AstroAttack is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation; either version 2 of the License, or
*	(at your option) any later version.
*	
*	AstroAttack is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*	
*	You should have received a copy of the GNU General Public License
*	along with AstroAttack; if not, write to the Free Software
*	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*	
*	$Id: AstroFrame.java 33 2006-12-27 23:15:05Z xdracox $
*/

/**	The AstroAttack package contains all the classes that belong to the
*	AstroAttack program.
*/
package AstroAttack;

import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;

import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;

/**	The AstroFrame class is a Frame that manages its own fullscreen and
*	display mode.
*/
public class AstroFrame extends Frame implements KeyListener, WindowListener
{
	/**	The display mode before it was changed. */
	private DisplayMode m_oldDisplay;
	/**	Graphics device used. */
	private GraphicsDevice m_device;
	
	/**	Constructs a new Frame and attempts fullscreen if tryFullscreen is true.
	*	@param	tryFullscreen	wether or not to attempt fullscreen
	*/
	public AstroFrame(boolean tryFullscreen)
	{
		super("AstroAttack - CoreTech Software");
		
		addWindowListener(this);
		addKeyListener(this);
		
		m_device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		boolean fullScreenAvailable = true;
		boolean displayModeAvailable = false;
		
		if ( tryFullscreen )
		{	// try fullscreen
			m_oldDisplay = m_device.getDisplayMode();
			
			Debug.println("Trying fullscreen:");
			if ( m_device.isFullScreenSupported() )
			{	// check if fullscreen is supported first...
				
				setUndecorated(true);
				m_device.setFullScreenWindow(this);
				
				if ( !m_device.isDisplayChangeSupported() )
				{
					Debug.println("\tdisplay change not supported, fullscreen not available");
					displayModeAvailable = false;
					fullScreenAvailable = false;
				}
				else
				{
					DisplayMode modes[] = m_device.getDisplayModes();
					
					for ( DisplayMode mode : modes )
					{
						if ( mode.getWidth() == 800 && mode.getHeight() == 600 )
						{
							m_device.setDisplayMode(mode);
							displayModeAvailable = true;
							break;
						}
					}
					
					if ( !displayModeAvailable )
					{
						Debug.println("\tdisplay mode 800x600 not available");
						fullScreenAvailable = false;
					}
					else
					{
						Debug.println("\tfullscreen enabled");
					}
				}
			}
			else
			{
				Debug.println("\tfull screen not available.");
				fullScreenAvailable = false;
			}
			
			if ( !fullScreenAvailable )
			{
				Debug.println("\tusing windowed mode");
				m_device.setFullScreenWindow(null);
				setUndecorated(false);
				setSize(800, 600);
			}
		}
		else
		{
			Debug.println("Using windowed mode:");
			setSize(800, 600);
			Debug.println("\tsize: 800x600");
		}
	}
	
	public void keyReleased(KeyEvent e)
	{
		if( e.getKeyCode() == KeyEvent.VK_ESCAPE )
		{
			windowClosing(null);
		}
	}
	
	public void windowClosing(WindowEvent e)
	{
		Debug.println("Window closing:");
		
		Engine engine = Engine.getEngine();
		
		if ( engine != null )
			engine.stop();
		
		if ( m_oldDisplay != null )
		{
			m_device.setDisplayMode(m_oldDisplay);
			m_device.setFullScreenWindow(null);
		}
		
		System.exit(0);
	}
	
	public void keyPressed(KeyEvent e)
	{
	}
	
	public void keyTyped(KeyEvent e)
	{
	}
	
	public void windowActivated(WindowEvent e)
	{
	}
	
	public void windowClosed(WindowEvent e)
	{
	}
	
	public void windowDeactivated(WindowEvent e)
	{
	}
	
	public void windowDeiconified(WindowEvent e)
	{
	}
	
	public void windowIconified(WindowEvent e)
	{
	}
	
	public void windowOpened(WindowEvent e)
	{
	}
}